
from rest_framework.views import APIView
from rest_framework.response import Response
from meiduo_admin.serializers.login_serializer import LoginSerializer

class LoginView(APIView):

    def post(self, request):
        # 1、调用序列化器完成数据的校验
        serializer = LoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        # 2、得到有效数据，构建响应参数
        token = serializer.validated_data.get('token')
        user = serializer.validated_data.get('user')
        return Response({
            'username': user.username,
            'user_id': user.id,
            'token': token
        })